"use strict";
let valGetParentContainer = function (element) {
    let $element = $(element);

    if ($element.closest('.form-group-sub').length > 0) {
        return $element.closest('.form-group-sub')
    } else if ($element.closest('.bootstrap-select').length > 0) {
        return $element.closest('.bootstrap-select')
    } else {
        return $element.closest('.form-group');
    }
};
/**
 * 处理select元素提示文字
 *
 * @param error {object} error
 * @returns {object} error
 */
let handleError = function (error) {
    if ('SELECT' === $(error.element).prop('nodeName').toUpperCase()) {
        error.message = error.message.replace('输入', '选择').replace('字符', '选项');
    }
    return error;
};
jQuery.validator.setDefaults({
    errorElement: 'div', //默认输入错误消息容器
    focusInvalid: false, // 不要聚焦最后一个无效的输入
    ignore: ':hidden',  // 验证所有字段
    showErrors: function () {
        let elements, error;

        for (let i = 0; this.errorList[i]; i++) {
            error = handleError(this.errorList[i]);
            this.settings.highlight.call(this, error.element, this.settings.errorClass, this.settings.validClass);
            this.showLabel(error.element, error.message);
        }
        if (this.errorList.length) {
            this.toShow = this.toShow.add(this.containers);
        }
        for (let i = 0; this.successList[i]; i++) {
            this.showLabel(this.successList[i]);
        }
        for (let i = 0, elements = this.validElements(); elements[i]; i++) {
            this.settings.unhighlight.call(this, elements[i], this.settings.errorClass, this.settings.validClass);
        }
        this.toHide = this.toHide.not(this.toShow);
        this.hideErrors();
        this.addWrapper(this.toShow).show();
    },
    errorPlacement: function (error, element) {
        let $element = $(element);

        let group = valGetParentContainer(element);
        let help = group.find('.form-text');

        if (group.find('.valid-feedback, .invalid-feedback').length !== 0) {
            return;
        }

        $element.addClass('is-invalid');
        error.addClass('invalid-feedback');

        if (help.length > 0) {
            help.before(error);
        } else {
            if ($element.closest('.bootstrap-select').length > 0) {     //Bootstrap select
                $element.closest('.bootstrap-select').find('.btn.dropdown-toggle').after(error);
            } else if ($element.closest('.input-group').length > 0) {   //Bootstrap group
                $element.after(error);
            } else {                                                   //Checkbox & radios
                if ($element.is(':checkbox')) {
                    $element.closest('.e-checkbox').find('> span').after(error);
                } else {
                    $element.after(error);
                }
            }
        }
    },

    highlight: function (element) {
        let group = valGetParentContainer(element);
        group.addClass('validate');
        group.addClass('is-invalid');
        $(element).removeClass('is-valid');
    },

    unhighlight: function (element) {
        let group = valGetParentContainer(element);
        group.removeClass('validate');
        group.removeClass('is-invalid');
        $(element).removeClass('is-invalid');
    },

    success: function (label, element) {
        let group = valGetParentContainer(element);
        group.removeClass('validate');
        group.find('.invalid-feedback').remove();
    }
});

jQuery.validator.addMethod("email", function (value, element) {
    return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(value);
}, "请输入有效的电子邮件地址");
